package com.java110.mall.cart.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.dto.user.UserDto;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.mall.cart.bmo.housekeepingServPool.IDeleteHousekeepingServPoolBMO;
import com.java110.mall.cart.bmo.housekeepingServPool.IGetHousekeepingServPoolBMO;
import com.java110.mall.cart.bmo.housekeepingServPool.ISaveHousekeepingServPoolBMO;
import com.java110.mall.cart.bmo.housekeepingServPool.IUpdateHousekeepingServPoolBMO;
import com.java110.dto.housekeeping.HousekeepingServPoolDto;
import com.java110.po.housekeeping.HousekeepingServPoolPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/housekeepingServPool")
public class HousekeepingServPoolApi {

    @Autowired
    private ISaveHousekeepingServPoolBMO saveHousekeepingServPoolBMOImpl;

    @Autowired
    private IUpdateHousekeepingServPoolBMO updateHousekeepingServPoolBMOImpl;

    @Autowired
    private IDeleteHousekeepingServPoolBMO deleteHousekeepingServPoolBMOImpl;

    @Autowired
    private IGetHousekeepingServPoolBMO getHousekeepingServPoolBMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /housekeepingServPool/saveHousekeepingServPool
     * @path /app/housekeepingServPool/saveHousekeepingServPool
     */
    @RequestMapping(value = "/saveHousekeepingServPool", method = RequestMethod.POST)
    public ResponseEntity<String> saveHousekeepingServPool(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "cartId", "请求报文中未包含cartId");
        Assert.hasKeyAndValue(reqJson, "servId", "请求报文中未包含servId");
        Assert.hasKeyAndValue(reqJson, "servName", "请求报文中未包含servName");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含state");
        Assert.hasKeyAndValue(reqJson, "objType", "请求报文中未包含objType");
        Assert.hasKeyAndValue(reqJson, "objId", "请求报文中未包含objId");
        Assert.hasKeyAndValue(reqJson, "objName", "请求报文中未包含objName");
        Assert.hasKeyAndValue(reqJson, "applyName", "请求报文中未包含applyName");
        Assert.hasKeyAndValue(reqJson, "applyTel", "请求报文中未包含applyTel");
        Assert.hasKeyAndValue(reqJson, "appointmentTime", "请求报文中未包含appointmentTime");


        HousekeepingServPoolPo housekeepingServPoolPo = BeanConvertUtil.covertBean(reqJson, HousekeepingServPoolPo.class);
        return saveHousekeepingServPoolBMOImpl.save(housekeepingServPoolPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /housekeepingServPool/updateHousekeepingServPool
     * @path /app/housekeepingServPool/updateHousekeepingServPool
     */
    @RequestMapping(value = "/updateHousekeepingServPool", method = RequestMethod.POST)
    public ResponseEntity<String> updateHousekeepingServPool(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "cartId", "请求报文中未包含cartId");
        Assert.hasKeyAndValue(reqJson, "servId", "请求报文中未包含servId");
        Assert.hasKeyAndValue(reqJson, "servName", "请求报文中未包含servName");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "state", "请求报文中未包含state");
        Assert.hasKeyAndValue(reqJson, "objType", "请求报文中未包含objType");
        Assert.hasKeyAndValue(reqJson, "objId", "请求报文中未包含objId");
        Assert.hasKeyAndValue(reqJson, "objName", "请求报文中未包含objName");
        Assert.hasKeyAndValue(reqJson, "applyName", "请求报文中未包含applyName");
        Assert.hasKeyAndValue(reqJson, "applyTel", "请求报文中未包含applyTel");
        Assert.hasKeyAndValue(reqJson, "appointmentTime", "请求报文中未包含appointmentTime");
        Assert.hasKeyAndValue(reqJson, "poolId", "poolId不能为空");


        HousekeepingServPoolPo housekeepingServPoolPo = BeanConvertUtil.covertBean(reqJson, HousekeepingServPoolPo.class);
        return updateHousekeepingServPoolBMOImpl.update(housekeepingServPoolPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /housekeepingServPool/deleteHousekeepingServPool
     * @path /app/housekeepingServPool/deleteHousekeepingServPool
     */
    @RequestMapping(value = "/deleteHousekeepingServPool", method = RequestMethod.POST)
    public ResponseEntity<String> deleteHousekeepingServPool(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopId", "shopId不能为空");

        Assert.hasKeyAndValue(reqJson, "poolId", "poolId不能为空");


        HousekeepingServPoolPo housekeepingServPoolPo = BeanConvertUtil.covertBean(reqJson, HousekeepingServPoolPo.class);
        return deleteHousekeepingServPoolBMOImpl.delete(housekeepingServPoolPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 小区ID
     * @returnqueryHousekeepingFeeType
     * @serviceCode /housekeepingServPool/queryHousekeepingServPool
     * @path /app/housekeepingServPool/queryHousekeepingServPool
     */
    @RequestMapping(value = "/queryHousekeepingServPool", method = RequestMethod.GET)
    public ResponseEntity<String> queryHousekeepingServPool(@RequestParam(value = "shopId") String shopId,
                                                            @RequestParam(value = "servName", required = false) String servName,
                                                            @RequestParam(value = "poolId", required = false) String poolId,
                                                            @RequestParam(value = "state", required = false) String state,
                                                            @RequestParam(value = "applyName", required = false) String applyName,
                                                            @RequestParam(value = "applyTel", required = false) String applyTel,
                                                            @RequestParam(value = "page") int page,
                                                            @RequestParam(value = "row") int row) {
        HousekeepingServPoolDto housekeepingServPoolDto = new HousekeepingServPoolDto();
        housekeepingServPoolDto.setPage(page);
        housekeepingServPoolDto.setRow(row);
        housekeepingServPoolDto.setShopId(shopId);
        housekeepingServPoolDto.setServName(servName);
        housekeepingServPoolDto.setState(state);
        housekeepingServPoolDto.setApplyName(applyName);
        housekeepingServPoolDto.setApplyTel(applyTel);
        housekeepingServPoolDto.setPoolId(poolId);
        return getHousekeepingServPoolBMOImpl.get(housekeepingServPoolDto);
    }

    /**
     * 查询 家政待办
     *
     * @param shopId 小区ID
     * @return
     * @serviceCode /housekeepingServPool/queryStaffServ
     * @path /app/housekeepingServPool/queryStaffServ
     */
    @RequestMapping(value = "/queryStaffServ", method = RequestMethod.GET)
    public ResponseEntity<String> queryStaffServ(
            @RequestHeader(value = "user-id") String userId,
            @RequestParam(value = "shopId") String shopId,
            @RequestParam(value = "servName", required = false) String servName,
            @RequestParam(value = "state", required = false) String state,
            @RequestParam(value = "cartId", required = false) String cartId,
            @RequestParam(value = "applyName", required = false) String applyName,
            @RequestParam(value = "applyTel", required = false) String applyTel,
            @RequestParam(value = "page") int page,
            @RequestParam(value = "row") int row) {
        HousekeepingServPoolDto housekeepingServPoolDto = new HousekeepingServPoolDto();
        housekeepingServPoolDto.setPage(page);
        housekeepingServPoolDto.setRow(row);
        housekeepingServPoolDto.setShopId(shopId);
        housekeepingServPoolDto.setServName(servName);
        housekeepingServPoolDto.setStaffId(userId);
        housekeepingServPoolDto.setState(state);
        housekeepingServPoolDto.setCartId(cartId);
        housekeepingServPoolDto.setApplyName(applyName);
        housekeepingServPoolDto.setApplyTel(applyTel);
        return getHousekeepingServPoolBMOImpl.queryStaffServ(housekeepingServPoolDto);
    }

    /**
     * 查询 家政已办
     *
     * @param shopId 小区ID
     * @return
     * @serviceCode /housekeepingServPool/queryStaffFinishServ
     * @path /app/housekeepingServPool/queryStaffFinishServ
     */
    @RequestMapping(value = "/queryStaffFinishServ", method = RequestMethod.GET)
    public ResponseEntity<String> queryStaffFinishServ(
            @RequestHeader(value = "user-id") String userId,
            @RequestParam(value = "shopId") String shopId,
            @RequestParam(value = "servName", required = false) String servName,
            @RequestParam(value = "cartId", required = false) String cartId,
            @RequestParam(value = "applyName", required = false) String applyName,
            @RequestParam(value = "applyTel", required = false) String applyTel,
            @RequestParam(value = "state", required = false) String state,
            @RequestParam(value = "page") int page,
            @RequestParam(value = "row") int row) {
        HousekeepingServPoolDto housekeepingServPoolDto = new HousekeepingServPoolDto();
        housekeepingServPoolDto.setPage(page);
        housekeepingServPoolDto.setRow(row);
        housekeepingServPoolDto.setShopId(shopId);
        housekeepingServPoolDto.setServName(servName);
        housekeepingServPoolDto.setStaffId(userId);
        housekeepingServPoolDto.setState(state);
        housekeepingServPoolDto.setCartId(cartId);
        housekeepingServPoolDto.setApplyName(applyName);
        housekeepingServPoolDto.setApplyTel(applyTel);
        return getHousekeepingServPoolBMOImpl.queryStaffFinishServ(housekeepingServPoolDto);
    }

    /**
     * 保存电话维修服务
     *
     * @param reqJson
     * @return
     * @serviceCode /housekeepingServPool/saveTelHousekeepingServ
     * @path /app/housekeepingServPool/saveTelHousekeepingServ
     */
    @RequestMapping(value = "/saveTelHousekeepingServ", method = RequestMethod.POST)
    public ResponseEntity<String> saveTelHousekeepingServ(
            @RequestHeader(value = "user-id") String userId,
            @RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "servId", "请求报文中未包含servId");
        Assert.hasKeyAndValue(reqJson, "servName", "请求报文中未包含servName");
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "applyName", "请求报文中未包含applyName");
        Assert.hasKeyAndValue(reqJson, "applyTel", "请求报文中未包含applyTel");
        Assert.hasKeyAndValue(reqJson, "appointmentTime", "请求报文中未包含appointmentTime");
        if (reqJson.containsKey("housekeepingServs") && !StringUtil.isEmpty(reqJson.getString("housekeepingServs"))) {
            Object object = reqJson.get("housekeepingServs");
            JSONArray housekeepingServs = JSONArray.parseArray(JSON.toJSONString(object));
            //获取服务id
            String servId = reqJson.getString("servId");
            for (int i = 0; i < housekeepingServs.size(); i++) {
                //获取服务id
                String servId1 = housekeepingServs.getJSONObject(i).getString("servId");
                if (servId.equals(servId1)) {
                    reqJson.put("areaCode", housekeepingServs.getJSONObject(i).getString("areaCode"));
                }
            }
        }

        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        Assert.listOnlyOne(userDtos,"用户未登录");

        HousekeepingServPoolPo housekeepingServPoolPo = BeanConvertUtil.covertBean(reqJson, HousekeepingServPoolPo.class);
        return saveHousekeepingServPoolBMOImpl.saveTel(housekeepingServPoolPo, userId, userDtos.get(0).getName());
    }

}
